#include<bits/stdc++.h>
#define M 200005
using namespace std;
int n, m, b[M], c[M], K, id[M], bl[M << 2], to[M], a[M], sum[M], nc[M], seq[M], fl[M];
char s[30][M];
int fin(int l, int r, int dep, int ps, int w)
{
    if(l == r) return l;
    int mid = (l + r) >> 1;
    int lp = fin(l, mid, dep - 1, ps * 2, w);
    int rp = fin(mid + 1, r, dep - 1, ps * 2 + 1, w);
    int ck = s[K - (w - dep)][ps] - '0';
    //cout << l << " " << r << " " << ck << endl;
    if(ck == 0)
    {
        if(seq[lp] >= dep) return lp;
        else return rp;
    }
    else
    {
        if(seq[rp] >= dep) return rp;
        else return lp;
    }
}
int calc(int w)
{
    return fin(1, 1 << w, w, 0, w);
}
void dfs(int step, int w)
{
    //cout << step << " " << w << endl;
    if(step == (1 << w) + 1)
    {
        //for(int i = 1; i <= (1 << w); i++) cout << seq[i] << " ";
        //cout << calc(w) << endl;
        fl[calc(w)] = 1;
        return;
    }
    for(int j = 0; j <= w; j++) seq[step] = j, dfs(step + 1, w);
}
void solve()
{
    long long xorn = 0;
    for(int i = 1; i <= m; i++)
    {
        //cout << i << endl;
        for(int j = 1; j <= c[i]; j++) seq[j] = a[j];
        int w = 0;
        while(1)
        {
            if((1 << w) >= c[i]) break;
            w++;
        }
        //cout << w << endl;
        for(int j = 1; j <= (1 << w); j++) fl[j] = 0;
        dfs(c[i] + 1, w);
        long long ret = 0;
        for(int j = 1; j <= (1 << w); j++) ret += fl[j] * j;// cout << j << " " << fl[j] << endl;
        xorn ^= (1ll * i * ret);
    }
    printf("%lld\n", xorn);
}
signed main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
    for(int i = 1; i <= m; i++) scanf("%d", &c[i]);
    //cout << 1 << endl;
    while(1)
    {
        if((1 << K) >= n) break;
        K++;
    }
    for(int i = 1; i <= K; i++)
    {
        scanf("%s", s[i]);
    }
    int T;
    scanf("%d", &T);
    for(int i = 1; i <= T; i++)
    {
        int nx[4];
        scanf("%d%d%d%d", &nx[0], &nx[1], &nx[2], &nx[3]);
        for(int j = 1; j <= n; j++) a[j] = b[j] ^ nx[j % 4];
        solve();
    }
    return 0;
}
